package com.hombio.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hombio.UIBean.MedicineTakeUI;
import com.hombio.service.MedicineMSService;

import cn.edu.scau.cmi.hombio.domain.Employee;



@Controller
public class EmployeeController {
	
	@Autowired
	MedicineMSService medicineMSService;
	
	@RequestMapping("/login/go")
	public String goLogin(HttpSession session){
		if(session.getAttribute("employee")!=null){
			return "main";
		}else{
			
			return "login";
		}
	}
	
	@RequestMapping("/e/edit/password")
	public String goEditPassword(){
		return "password-edit";
	}
	
	@RequestMapping("/e/edit/password/do")
	@ResponseBody
	public void editPassword(Integer employeeId, String password){
		medicineMSService.editPassword(employeeId, password);
	}
	
	
	@RequestMapping("/logout/go")
	public String logout(HttpSession session){
		session.removeAttribute("employee");
		session.removeAttribute("msg");
		return "login";
	}
	
	@RequestMapping("/login/check")
	public String loginCheck(String tel,String pwd,HttpSession session,HttpServletResponse response){
		Employee employee = medicineMSService.login(tel, pwd);
		if(employee != null){
			Cookie userName = new Cookie("username", tel);
			Cookie password = new Cookie("password",pwd);
			response.addCookie(userName);
			response.addCookie(password);
			session.setAttribute("employee", employee);
			return "main";
		}else{
			session.setAttribute("msg", "账号不存在或者密码错误");
			return "login";
		}
		
	}
	
	
	@RequestMapping("/e/taskMedicine/{employeeId}")
	@ResponseBody
	public List<MedicineTakeUI> getMedicineTask(@PathVariable Integer employeeId){
		return medicineMSService.getTaskMedicineByEmployeeId(employeeId);
	}
	@RequestMapping("/e/cureEmployee/list")
	@ResponseBody
	public List<Employee> getCureOrPreventionEmployees(){
		return medicineMSService.getCureOrPreventionEmployees();
	}
	
}
